/*
 * Package il.ac.biu.cs.grossmm.impl.activeData
 * File SqlSynchTest.java
 * 
 * This is a part of presence service framework prototype. 
 * See javadoc for more information.
 *  
 * Copyright (C) 2006 Grossmann Mark
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.

 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
package il.ac.biu.cs.grossmm.impl.activeData;

import static org.junit.Assert.*;

import il.ac.biu.cs.grossmm.impl.activeData.SqlSynch.DeletedException;

import java.util.Random;
import java.util.concurrent.atomic.AtomicInteger;

import org.junit.Before;
import org.junit.Test;

public class SqlSynchTest {
	SqlSynch synch;

	AtomicInteger reads;

	AtomicInteger writes;

	final Random r = new Random();

	class Reader extends Thread {

		public void run() {
			try {
				for (int i = 0; i < 100; i++) {
					read();
					if (i % 2 == 0)
						sleep(i % 5 + 1);
				}
			} catch (InterruptedException e) {
			} catch (Exception e) {
				e.printStackTrace();
				fail(e.getMessage());
			}

		}

		/**
		 * @throws InterruptedException
		 * @throws DeletedException
		 */
		private void read() throws InterruptedException, DeletedException {
			if (synch.state == SqlSynch.State.PENDING)
				System.out.print("*");

			synch.readLock();

			try {
				System.out.print("R(");
				assertTrue(synch.state != SqlSynch.State.WRITING);

				assertEquals(synch.state.toString(), 0, writes.get());
				reads.incrementAndGet();

				sleep(r.nextInt(20));
				System.out.print(")R");

				reads.decrementAndGet();
			} finally {
				synch.readUnlock();
			}
		}
	}

	class Writter extends Thread {

		public void run() {
			try {
				for (int i = 0; i < 100; i++) {
					write();
					if (i % 2 == 0)
						sleep(i % 5 + 1);
				}
			} catch (InterruptedException e) {
			} catch (Exception e) {
				e.printStackTrace();
				fail(e.getMessage());
			}
		}

		/**
		 * @throws InterruptedException
		 * @throws DeletedException
		 */
		private void write() throws InterruptedException, DeletedException {
			synch.writeLock(null);

			try {
				System.out.print("\nW(");
				assertTrue(synch.state == SqlSynch.State.WRITING);

				int w = writes.incrementAndGet();
				assertEquals(1, w);

				assertEquals(0, reads.get());

				sleep(r.nextInt(20));

				assertEquals(0, reads.get());
				writes.decrementAndGet();
				System.out.print(")W.");
			} finally {
				synch.writeToNotifyRelock();
				try {
					System.out.print(".N(");
					assertTrue(synch.state == SqlSynch.State.NOTIFYING);
					sleep(r.nextInt(20));
					System.out.print(")N");
				} finally {
					synch.notifyUnlock();
				}
			}
		}
	}

	@Before
	public void setup() {
		synch = new SqlSynch();
		reads = new AtomicInteger(0);
		writes = new AtomicInteger(0);
	}

	@Test
	public void test1() throws InterruptedException {
		Thread[] threads = new Thread[10];

		for (int i = 0; i < threads.length; i++) {
			threads[i] = i % 5 != 0 ? new Reader() : new Writter();
			threads[i].start();
		}

		for (int i = 0; i < threads.length; i++)
			threads[i].join();
	}
}
